package com.sumainfor.common.alipayutils;

import org.apache.commons.lang.StringUtils;
import org.apache.http.message.BasicNameValuePair;


import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.TreeMap;

public class SignUtils {

//	private static final String PRV = (ClassLoader.getSystemResource("").toString()+ConfigUtils.getProperty("merchant_no") +"_prv.pem").substring(6);
//	private static final String PRV = "E:/JavaDemo/bin/888440058111002_prv.pem";
//	private static final String PUB = (ClassLoader.getSystemResource("").toString()+ConfigUtils.getProperty("merchant_no") +"_pub.pem").substring(6);
//	private static final String PUB = "E:/JavaDemo/bin/888440058111002_pub.pem";
//	private static final String PRV = "/Users/chengchunyu/Desktop/JavaDemo/src/888290058111340_prv.pem";
//	private static final String PUB = "/Users/chengchunyu/Desktop/JavaDemo/src/888290058111340_pub.pem";
//	private static final String PRV = "/Users/chengchunyu/Desktop/JavaDemo/src/885440059321286_prv.pem";//房租。房租押金
//	private static final String PUB = "/Users/chengchunyu/Desktop/JavaDemo/src/885440059321286_pub.pem";
    private static final String PRVS = "E:/idea/tangnest/tangnest-wx-tenant/src/888290058111340_prv.pem";//公共事业费
    private static final String PUBS = "E:/idea/tangnest/tangnest-wx-tenant/src/888290058111340_pub.pem";

    private static final String PRV = "E:/idea/tangnest/tangnest-wx-tenant/src/885440059321286_prv.pem";//房租
    private static final String PUB = "E:/idea/tangnest/tangnest-wx-tenant/src/885440059321286_pub.pem";

    /**
     * 房租请求数据
     * @param nvps
     * @return
     * @throws Exception
     */
    public static String signData(List<BasicNameValuePair> nvps) throws Exception {
        TreeMap<String, String> tempMap = new TreeMap<String, String>();
        for (BasicNameValuePair pair : nvps) {
            if (StringUtils.isNotBlank(pair.getValue())) {
                tempMap.put(pair.getName(), pair.getValue());
            }
        }
        StringBuffer buf = new StringBuffer();
        for (String key : tempMap.keySet()) {
            buf.append(key).append("=").append((String) tempMap.get(key)).append("&");
        }
        String signatureStr = buf.substring(0, buf.length() - 1);
//        String st = URLEncoder.encode(signatureStr,   "utf-8");	//编码
        System.out.println(PRV);
        String signData = RSAUtil.signByPrivate(signatureStr, RSAUtil.readFile(PRV, "UTF-8"), "UTF-8");
        System.out.println("请求数据：" + signatureStr + "&signature=" + signData);
        return signData;
    }

    /**
     * 公共事业费请求数据
     * @param nvps
     * @return
     * @throws Exception
     */
    public static String signDatas(List<BasicNameValuePair> nvps) throws Exception {
        TreeMap<String, String> tempMap = new TreeMap<String, String>();
        for (BasicNameValuePair pair : nvps) {
            if (StringUtils.isNotBlank(pair.getValue())) {
                tempMap.put(pair.getName(), pair.getValue());
            }
        }
        StringBuffer buf = new StringBuffer();
        for (String key : tempMap.keySet()) {
            buf.append(key).append("=").append((String) tempMap.get(key)).append("&");
        }
        String signatureStr = buf.substring(0, buf.length() - 1);
//        String st = URLEncoder.encode(signatureStr,   "utf-8");	//编码
        System.out.println(PRVS);
        String signData = RSAUtil.signByPrivate(signatureStr, RSAUtil.readFile(PRVS, "UTF-8"), "UTF-8");
        System.out.println("请求数据：" + signatureStr + "&signature=" + signData);
        return signData;
    }

    /**
     * 房租相应数据
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static boolean verferSignData(String str) throws UnsupportedEncodingException {
        System.out.println("响应数据：" + str);
        String data[] = str.split("&");
        StringBuffer buf = new StringBuffer();
        String signature = "";
        for (int i = 0; i < data.length; i++) {
            String tmp[] = data[i].split("=", 2);
            if ("signature".equals(tmp[0])) {
                signature = tmp[1];
            } else {
                buf.append(tmp[0]).append("=").append(tmp[1]).append("&");
            }
        }
        String signatureStr = buf.substring(0, buf.length() - 1);
//        String st = URLEncoder.encode(signatureStr, "utf-8");	//编码
        System.out.println("验签数据：" + signatureStr);
        return RSAUtil.verifyByKeyPath(signatureStr, signature,PUB, "UTF-8");
    }

    /**
     * 公共事业费响应数据
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static boolean verferSignDatas(String str) throws UnsupportedEncodingException {
        System.out.println("响应数据：" + str);
        String data[] = str.split("&");
        StringBuffer buf = new StringBuffer();
        String signature = "";
        for (int i = 0; i < data.length; i++) {
            String tmp[] = data[i].split("=", 2);
            if ("signature".equals(tmp[0])) {
                signature = tmp[1];
            } else {
                buf.append(tmp[0]).append("=").append(tmp[1]).append("&");
            }
        }
        String signatureStr = buf.substring(0, buf.length() - 1);
//        String st = URLEncoder.encode(signatureStr, "utf-8");	//编码
        System.out.println("验签数据：" + signatureStr);
        return RSAUtil.verifyByKeyPath(signatureStr, signature,PUBS, "UTF-8");
    }





}
